package com.aiju.cash;

import android.app.ActivityManager;
import android.app.Notification;
import android.content.Context;
import android.os.Process;

import com.aiju.base.aplication.BaseApplication;
import com.aiju.cash.core.api.LoginApi;
import com.aiju.utils.L;
import com.aiju.utils.PreferencesUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import cn.jpush.android.api.BasicPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface;
import okhttp3.Call;


/**
 * @author: 佐为
 * @date: 2016-11-11 15:12
 * @version: V1.0
 * @Description: 描述
 */
public class CashApplication extends BaseApplication {

    @Override
    public void onCreate() {
        String curProcess = getProcessName(this, Process.myPid());
        if (!getPackageName().equals(curProcess)) {
            return;
        }
        super.onCreate();
        JPushInterface.setDebugMode(true);    // 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);            // 初始化 JPush

        boolean vibrator = PreferencesUtils.getBoolean(this, "vibrator");
        BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(this);
        builder.statusBarDrawable = R.mipmap.icon_launcher;
        builder.notificationFlags = Notification.FLAG_AUTO_CANCEL;  //设置为自动消失
        if (vibrator) {
            builder.notificationDefaults = Notification.DEFAULT_VIBRATE | Notification.DEFAULT_VIBRATE;
        } else {
            builder.notificationDefaults = Notification.DEFAULT_LIGHTS;
        }
        JPushInterface.setPushNotificationBuilder(1, builder);
    }

    public static String getProcessName(Context cxt, int pid) {
        ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
        if (runningApps != null && !runningApps.isEmpty()) {
            for (ActivityManager.RunningAppProcessInfo procInfo : runningApps) {
                if (procInfo.pid == pid) {
                    return procInfo.processName;
                }
            }
        }

        return null;
    }

    @Override
    public void onTerminate() {
        L.d("onTerminate");
        new LoginApi().updateUserOnlineStatus(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {

            }
        });
        super.onTerminate();
    }
}
